<template>
  <div>
    <el-button type="primary" plain size="small" @click="add" icon="el-icon-upload2">上传</el-button>
    <el-button type="danger" plain size="small" @click="batchDel" icon="el-icon-delete">批量删除</el-button>
    <el-table
      :data="tableData"
      @selection-change="handleSelectionChange"
      style="width: 100%">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        prop="id"
        label="ID">
      </el-table-column>
      <el-table-column
        prop="role_name"
        label="名称">
      </el-table-column>
      <el-table-column
        prop="role_en"
        label="大小">
      </el-table-column>
      <el-table-column
        prop="description"
        label="发布人">
      </el-table-column>
      <el-table-column
        prop="description"
        label="发布时间">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.row)">编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="pageSizes"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="clearInfo">
      <el-form :model="form" :rules="rules" ref="dialogForm">
        <el-form-item label="标题" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="资源类型" :label-width="formLabelWidth" prop="file_type">
          <el-select v-model="form.type" placeholder="请选择类型" style="width: 100%">
            <el-option label="视频" value="video"></el-option>
            <el-option label="文本" value="text"></el-option>
            <el-option label="图片" value="picture"></el-option>
            <el-option label="其它" value="other"></el-option>
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="视频封面" :label-width="formLabelWidth" v-if="form.type=='video'">-->
        <!--          <el-upload-->
        <!--            action="http://upload-z0.qiniu.com"-->
        <!--            :accept="accept"-->
        <!--            list-type="picture"-->
        <!--            :limit=1-->
        <!--            :data="uptoken"-->
        <!--            :before-upload="beforeAvatarUpload"-->
        <!--            :on-remove="handleRemoveChange"-->
        <!--            :on-success="handleSuccessChange"-->
        <!--            :file-list="formData.smallModelPhoto">-->
        <!--            <el-button size="mini" type="primary">点击上传</el-button>-->
        <!--            <div slot="tip" class="el-upload__tip">只能上传jpg/png/gif文件，且单张不超过10M，缩略图只能传一张</div>-->
        <!--          </el-upload>-->
        <!--        </el-form-item>-->
        <el-form-item label="选择资源" :label-width="formLabelWidth">
          <el-upload
            class="upload-demo"
            action="http(s)://up-z0.qiniup.com"
            :limit="1"
            :http-request="customUpload"
            :on-success="handleSuccess"
            list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="upload">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {getOneRole, rolePage} from "@/api/role";
import * as qiniu from 'qiniu-js'

export default {
  name: 'index',
  data() {
    return {
      formData: {
        smallModelPhoto: [],
      },
      uptoken: {
        token: '',
        key: ""
      },
      accept: "image/jpeg,image/gif,image/png,image/bmp",
      imageUrl: '',
      title: '上传',
      tableData: [],
      multipleSelection: [],
      currentPage: 1,
      pageSizes: [5, 10, 15, 20],
      pageSize: 5,
      total: 0,
      rules: {
        name: [
          {required: true, message: '请输入资源名称', trigger: 'blur'},
        ],
        file_type: [
          {required: true, message: '请选择资源类型', trigger: 'blur'}
        ]
      },
      formLabelWidth: '80px',
      dialogFormVisible: false,  //  控制对话框的显示隐藏
      form: {
        name: '',
        type: '',
        videopic_url: '',
        file_url: ''
      }
    }
  },
  methods: {
    // 编辑
    handleEdit(row) {
      this.title = '编辑'
      this.dialogFormVisible = true
      getOneRole(row.id).then((res) => {
        this.form = res.data.role
      })
    },
    // 删除
    handleDelete(row) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {   // 确定
        let {id} = row
        // 发送异步请求
      })
    },
    // 复选 参数  val  表示当前选中行的数据
    handleSelectionChange(val) {
      this.multipleSelection = val.map(row => row.id)
    },
    // 批量删除
    batchDel() {
      if (this.multipleSelection.length == 0) {
        this.$notify.warning('请至少选择一条数据')
        return
      }
    },
    // 新增
    add() {
      this.dialogFormVisible = true
    },
    upload() {

    },
    handleSizeChange(val) {
      this.pageSize = val

    },
    handleCurrentChange(val) {
      this.currentPage = val

    },
    //关闭弹窗时 清除表单数据
    clearInfo() {
      this.form = {}
      this.$refs.dialogForm.clearValidate()
    },
    rolePageData() {
      rolePage({page: this.currentPage, size: this.pageSize})
        .then((res) => {
          this.tableData = res.tableData
          this.total = res.total
        })
    },
    customUpload(info) {
      console.log(info.file)
      const token = 'u48RMMuTxcgGpA5l9vmZiRypgOYFsUKmAyBVYeic:2d_IzoG4BU-onR5iyQrn7NGQNNs=:eyJzY29wZSI6Imh1aXh1ZToiLCJkZWFkbGluZSI6MTc0ODA4MjA2N30='
      const key = ''
      const putExtra = {};
      const config = {}
      const observable = qiniu.upload(info.file, key, token, putExtra, config)
      const observer = {
        next(res) {
          console.log('res', res)
        },
        error(err) {
          // ...
        },
        complete(res) {
          // ...
          console.log(res)
        }
      }
      const subscription = observable.subscribe(observer) // 上传开始
    },
    handleSuccess() {

    }
  },
  created() {
    this.rolePageData()
  }
}
</script>
<style scoped lang="scss">


</style>
